//
//  ThridSearchController.m
//  quickLook
//
//  Created by J on 15/8/31.
//  Copyright (c) 2015年 JZJ. All rights reserved.
//

#import "ThridSearchController.h"

#import "DownLoad.h"

#import "Header.h"
#import "Model.h"
#import "NavisearchCell.h"
@interface ThridSearchController ()<UISearchBarDelegate,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>


@property (nonatomic,copy) NSArray *dataArray;
@property(nonatomic,strong)UITableView* tableView;



@end

@implementation ThridSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(pop)];
    
    
    
    self.view.backgroundColor=[UIColor whiteColor];
    
        //Set to titleView
    [self.navigationItem.titleView sizeToFit];
    self.navigationItem.title = _searchText;
    
    [self createUI];
    
    
    self.navigationItem.title=_searchText;
    
    
    
    
    


}

- (void)createUI

{
    
    
    
    _tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
    [self.view addSubview:_tableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self.tableView setTableFooterView:v];
    
    
    
}


#pragma mark --UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (_searchArray.count==0) {
        
        UIImageView * imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        
        
        imageView.image=[UIImage imageNamed:@"NoNet.png"];
        
        [_tableView addSubview:imageView];
        
        
    }
    
  

    
    return _searchArray.count;
    
    
    

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NavisearchCell* cell=[tableView dequeueReusableCellWithIdentifier:@"ID"];
    
    if (!cell) {
        cell=[[NSBundle mainBundle]loadNibNamed:@"NavisearchCell" owner:nil options:nil][0];
        
        _tableView.tableFooterView = [[UIView alloc]init];
        
        
    }
    
    
    
    Model* model=_searchArray[indexPath.row];
    
    [cell refreshWithModel:model WithIndexPath:indexPath];
  
    
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

//    Model* model=_dataArray[indexPath.row];

//    JZJLog(@"%@",model.id);


}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    
    
    
    
   }


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
    
    
    
    [DownLoad getFinderNavigationBarDataSearchWithUrl:[searchBar.text URLEncode] andBlock:^(id obj, NSError * error) {
        
        
        
        _dataArray=  obj[@"data"][@"topics"];
        
        if (_dataArray==nil) {
            
            
            
            
            self.view.backgroundColor=[UIColor orangeColor];
            
            
        }
        
        
        [_tableView reloadData];
        
    }];
    
    
    
    
}






    - (void)pop {
        
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
        
    }


@end
